<?php
namespace app\controllers;

use app\models\PaytypeRecorder;

class PaytypeController extends BaseController{
    //获取
    public function actionOne(){
        $post = \Yii::$app->request->post();
        $type = intval($post['pay_type']);
        $model = PaytypeRecorder::find()->where('pay_type=:type',[':type' => $type])->asArray()->one();
        if(empty($model)){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result,JSON_NUMERIC_CHECK);
        }
        $model['pay_content'] = json_decode($model['pay_content'],true);
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }

    //添加
    public function actionAdd(){
        $post = \Yii::$app->request->post('form');
        $type = intval($post['pay_type']);
        $pay_content = json_encode($post['pay_content']);
        $model = PaytypeRecorder::find()->where('pay_type=:type',[':type' => $type])->one();
        if(empty($model)){
            $model = new PaytypeRecorder();
        }
        $pay_name_arr = \Yii::$app->params['pay_name_arr'];
        $pay_name = $pay_name_arr[$type];
        $model->pay_type = $type;
        $model->pay_name = $pay_name;
        $model->is_pay = intval($post['is_pay']);
        $model->pay_content = $pay_content;
        $model->pay_info = $post['pay_info'];

        $model->addtime = time();
        $model->save(false);
        $result = ['result'=>1,'data'=>"操作成功"];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }
}
